added Feb 2001 SDK
[windows-sources.git] / shared source / wpf / src / host / shimimpl / version.cxx
blob182a244ab3cc268bdd8fc5b6278c2794d5166628
1 //------------------------------------------------------------------------
2 //
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 //
5 // Description:
6 // Implements the connection to PHDLL (PresentationHostDLL.dll v3 or PresentationHost_vX.dll for v4+).
7 //
8 // History:
9 // 2005/05/09 - [....]
10 // Created
11 // 2007/09/20-[....]
12 // Ported Windows->DevDiv. See SourcesHistory.txt.
14 //------------------------------------------------------------------------
16 #include "PreCompiled.hxx"
17 #include "Version.hxx"
18 #include "ActivationContext.hxx"
21 CVersion::CVersion(__in_ecount(1) LPCWSTR pswzVersion):
22 m_hInstance(0), m_bIsValid(false), m_bIsAttached(false),
23 m_dwMajor(0), m_dwMinor(0), m_dwBuild(0),
24 m_pfnActivate(0), m_pfnDeactivate(0), m_pfnForwardTranslateAccelerator(0),
25 m_pfnSaveToHistory(0), m_pfnLoadFromHistory(0)
27 if (pswzVersion)
29 SetValue(pswzVersion);
30 ParseVersion(pswzVersion);
34 int CVersion::CompareTo(__in_ecount(1) CVersion* pOther)
36 int nResult = this->m_dwMajor - pOther->m_dwMajor;
38 if (nResult == 0)
40 nResult = this->m_dwMinor - pOther->m_dwMinor;
43 if (nResult == 0)
45 nResult = this->m_dwBuild - pOther->m_dwBuild;
48 return nResult;
51 HRESULT CVersion::Attach()
53 EventWriteWpfHostUm_VersionAttach();
55 if(m_bIsValid && GetLibraryPath() != NULL)
57 m_hInstance = LoadLibrary(GetLibraryPath());
59 if(m_hInstance != NULL)
61 m_pfnActivate = (ActivatePfn) GetProcAddress(m_hInstance, "Activate");
62 m_pfnDeactivate = (DeactivatePfn) GetProcAddress(m_hInstance, "Deactivate");
63 m_pfnForwardTranslateAccelerator = (ForwardTranslateAcceleratorPfn) GetProcAddress(m_hInstance, "ForwardTranslateAccelerator");
64 m_pfnSaveToHistory = (SaveToHistoryPfn) GetProcAddress(m_hInstance, "SaveToHistory");
65 m_pfnLoadFromHistory = (LoadFromHistoryPfn) GetProcAddress(m_hInstance, "LoadFromHistory");
67 m_bIsAttached =
68 m_pfnActivate &&
69 m_pfnDeactivate &&
70 m_pfnForwardTranslateAccelerator &&
71 m_pfnSaveToHistory &&
72 m_pfnLoadFromHistory;
74 // Dev10.582711 - CRT initialization for PresentationCore v3 fails when hosted by PresentationHost v4.
75 // This is an involved issue, with a history going back to v3.5 SP1.
76 // See \\ddindex2\sources2\orc----p\wpf\src\core\crtinit.cpp for the initial issue and hacky
77 // workaround and the above bug for the unfortunate flaw that cropped up in v4.
78 // The essence of the patch here is that we apply a Fusion activation context for loading the VC 8 CRT
79 // based on the manifest embedded in PHDLL v3, which is the same as what PresentationCore.dll has
80 // (as long as they are the same build flavor--this was a caveat about the original workaround too).
81 // This activation context needs to be active only when PresentationCore (v3) is doing its
82 // initialization and binding to msvcr80.dll, but it doesn't hurt to keep it active for the whole
83 // lifetime of the process. In some sense this gives v3 a more compatible runtime environment
84 // because PH v3 used to have that manifest reference to the VC 8 CRT.
85 if(m_dwMajor == 3)
87 ActivationContext actctx;
88 if(!actctx.Create(GetLibraryPath()) || !actctx.Activate(false))
90 ASSERT(false);
96 return m_bIsAttached ? S_OK : E_FAIL;
99 void CVersion::ParseVersion(__in_ecount(1) LPCWSTR pswzVersion)
101 DWORD dwNumber[3] = {0,0,0};
102 // nIndex is used in a loop bounded by a constant (3). Since it is safely bounded we can declare it as __bound to avoid PREFast warnings
103 __bound UINT nIndex = 0;
105 m_bIsValid = TRUE;
107 BOOL bGotOpenQuote = FALSE;
109 WCHAR* pwcNext = (WCHAR*)pswzVersion;
111 // Initial quote is allowed and ignored
112 if (*pwcNext == L'\"')
114 ++pwcNext;
115 bGotOpenQuote = TRUE;
118 // Intial "v" is allowed, and ignored
119 if (*pwcNext == 'v' || *pwcNext == 'V')
121 ++pwcNext;
124 while (*pwcNext && nIndex < 3)
126 if (*pwcNext == '.')
128 ++nIndex;
130 else if (iswdigit(*pwcNext))
132 dwNumber[nIndex] *= 10;
133 dwNumber[nIndex] += *pwcNext - '0';
135 else
137 // Close quote (if it is the last character) is allowed and ignored
138 if (bGotOpenQuote && *pwcNext == L'\"')
140 ++pwcNext;
141 if (*pwcNext)
143 m_bIsValid = FALSE;
144 break;
147 else
149 m_bIsValid = FALSE;
150 break;
153 ++pwcNext;
156 if (m_bIsValid)
158 m_dwMajor = dwNumber[0];
159 m_dwMinor = dwNumber[1];
160 m_dwBuild = dwNumber[2];
162 else
164 m_dwMajor = 0;
165 m_dwMinor = 0;
166 m_dwBuild = 0;
170 void CVersion::Activate(__in_ecount(1) const ActivateParameters* pParameters, __deref_out_ecount(1) LPUNKNOWN* ppInner)
172 EventWriteWpfHostUm_VersionActivateStart();
174 m_pfnActivate(pParameters, ppInner); //[supposed to invoke Watson on failure]
176 EventWriteWpfHostUm_VersionActivateEnd();
179 void CVersion::Deactivate()
181 m_pfnDeactivate();
184 HRESULT CVersion::ForwardTranslateAccelerator(__in_ecount(1) MSG* pMsg)
186 return m_pfnForwardTranslateAccelerator(pMsg, VARIANT_FALSE/*the app wasn't given a chance*/);
189 HRESULT CVersion::SaveToHistory(__in_ecount(1) IStream* pHistoryStream)
191 return m_pfnSaveToHistory(pHistoryStream);
194 HRESULT CVersion::LoadFromHistory(__in_ecount(1) IStream* pHistoryStream, __in_ecount(1) IBindCtx* pBindCtx)
196 return m_pfnLoadFromHistory(pHistoryStream, pBindCtx);